# login_page.py
from selenium.webdriver.common.by import By
from .base_page import BasePage
import allure
import logging

logger = logging.getLogger(__name__)

class LoginPage(BasePage):
    # 定位器
    LOGIN_LINK = (By.XPATH, "//a[contains(text(), '登陆')]")
    USERNAME_INPUT = (By.ID, 'number')
    PASSWORD_INPUT = (By.XPATH, "//input[@id='password']")
    LOGIN_BUTTON = (By.XPATH, "//input[@id='logon_btn']")
    WELCOME_MESSAGE = (By.XPATH, "//p[contains(text(),'欢迎您，李四')]")
    LOGOUT_LINK = (By.XPATH, "//a[contains(text(), '退出')]")
    ERROR_MESSAGE = (By.XPATH, "//div[@class='user_error']")
    search_button=(By.XPATH, "//span[@class='glyphicon glyphicon-search']")
    shangpin_input=(By.XPATH, "//input[@id='search']")
    name_appear=(By.XPATH, "//a[contains(text(),'追风筝的人')]")
    name_erro=(By.XPATH, "//p[contains(text(),'抱歉，没有找到相关商品')]")
    login_log=(By.XPATH, "//p[@class='text-center']")
    
    def __init__(self, driver):
        super().__init__(driver)
    
    @allure.step("打开登录页面")
    def open_login_page(self):
        """打开登录页面"""
        logger.info("打开登录页面")
        self.click(*self.LOGIN_LINK)
    
    @allure.step("输入用户名: {username} 和密码: {password}")
    def enter_credentials(self, username, password):
        """输入用户名和密码"""
        logger.info(f"输入用户名: {username} 和密码: {password}")
        self.send_keys(*self.USERNAME_INPUT, username)
        self.send_keys(*self.PASSWORD_INPUT, password)
    
    @allure.step("点击登录按钮")
    def click_login_button(self):
        """点击登录按钮"""
        logger.info("点击登录按钮")
        self.click(*self.LOGIN_BUTTON)
    
    @allure.step("获取欢迎消息")
    def get_welcome_message(self):
        """获取欢迎消息"""
        return self.get_text(*self.WELCOME_MESSAGE)
    
    @allure.step("获取错误消息")
    def get_error_message(self):
        """获取错误消息"""
        return self.get_text(*self.ERROR_MESSAGE)
    
    @allure.step("退出登录")
    def logout(self):
        """退出登录"""
        logger.info("退出登录")
        self.click(*self.LOGOUT_LINK)
    
    @allure.step("使用用户名 {username} 和密码 {password} 登录")
    def login(self, username, password):
        """完整的登录流程"""
        self.open_login_page()
        self.enter_credentials(username, password)
        self.click_login_button()



    @allure.step("输入商品名称: {shangpin}")
    def enter_credential(self, shangpin):
        """输入用户名和密码"""
        logger.info(f"输入商品名称: {shangpin}")
        self.send_keys(*self.shangpin_input, shangpin)

    @allure.step("点击搜索按钮")
    def click_search_button(self):
        """点击搜索按钮"""
        logger.info("点击搜索按钮")
        self.click(*self.search_button)
        

    @allure.step("获取商品信息")
    def get_shangpin_message(self):
        """获取商品信息"""
        return self.get_text(*self.name_appear)
    
    @allure.step("获取错误消息")
    def get_error_shangpin(self):
        """获取错误消息"""
        return self.get_text(*self.name_erro)
    
    def research(self, shangpin):
        """完整的搜索流程"""
        self.enter_credential(shangpin)
        self.click_search_button()